<!--
 * @Author: your name 实时监测
 * @Date: 2021-12-29 21:52:08
 * @LastEditTime: 2022-01-06 11:50:09
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \am-ui\src\pages\am\monitor\RealTime.vue
-->
<template>
  <div class="realTime">
    <div class="content">
      <div class="left">
        <el-input v-model="keyWords" placeholder="请输入工程关键字"></el-input>
        <!-- 工程设备树 -->
        <el-tree
          :data="edTree"
          :props="props"
          @node-click="handleNodeClick"
        ></el-tree>
      </div>
      <div class="right">
        <div v-show="todayData.length == 0" class="notice">
          请先选择工程下的设备
        </div>
        <div v-show="todayData.length > 0">
          <!-- 标签页 -->
          <el-tabs v-model="activeName" @tab-click="tagHandleClick">
            <el-tab-pane label="tsp" name="tsp"></el-tab-pane>
            <el-tab-pane label="pm10" name="pm10"></el-tab-pane>
            <el-tab-pane label="pm2.5" name="pm25"></el-tab-pane>
            <el-tab-pane label="温度" name="temperature"></el-tab-pane>
            <el-tab-pane label="湿度" name="humidity"></el-tab-pane>
          </el-tabs>
          <!-- 图表容器 -->
          <div ref="realTime" id="realTime" style="height: 500px"></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { get } from "@/utils/request.js";
import moment from "moment";
import { Line } from "@antv/g2plot";
export default {
  data() {
    return {
      edTree: [],
      props: {
        label: "name",
        value: "id",
      },
      params: {
        page: 1,
        pageSize: 50,
      },
      todayData: [],
      activeName: "tsp",
      chart: null,
      keyWords: "",
    };
  },
  computed: {
    chartData() {
      return this.todayData.map((item) => {
        return {
          date: moment(item.insert_time).format("hh:mm"),
          [this.activeName]: parseInt(item[this.activeName]),
        };
      });
    },
  },
  methods: {
    // 图表初始化方法
    loadCharts() {
      // 判断图表是否存在，如果存在，先销毁
      if (this.chart) {
        this.chart.destroy();
      }
      // 第一步生成图表实例，并配置配置项
      this.chart = new Line("realTime", {
        data: this.chartData,
        padding: "auto",
        xField: "date",
        yField: this.activeName,
      });
      // 第二步：开始绘制图表
      this.chart.render();
    },
    // tag点击回调
    tagHandleClick() {
      // 点击tags标签时，重载图表数据
      this.chart.update({
        data: this.chartData,
        yField: this.activeName,
      });
    },
    // 树形节点点击事件
    handleNodeClick(data) {
      // 判断用户点击的是否是设备，点击工程时不查询数据
      if (data.engineer_id) {
        this.params = {
          ...this.params,
          engineer_id: data.engineer_id,
          device_id: data.id,
        };
        // 调用请求数据的方法
        this.queryTodayData();
      }
    },
    // 查询工程设备树
    findEngineerDeviceTree() {
      get("/engineer/findEngineerDeviceTree").then((res) => {
        this.edTree = res.data;
      });
    },
    // 根据工程设备ID检索当天设备数据
    queryTodayData() {
      get("/monitor/pageQueryTodayData", this.params).then((res) => {
        this.todayData = res.data.list;
        // 调用初始化图表的方法
        this.loadCharts();
      });
    },
  },
  created() {
    // 查询工程设备树
    this.findEngineerDeviceTree();
  },
};
</script>
<style scoped>
.content {
  display: flex;
}
.left {
  width: 30%;
  border-right: 1px solid #b9b7b7;
  padding-right: 20px;
  margin-right: 20px;
}
.right {
  width: 70%;
}
.notice {
  height: 500px;
  line-height: 500px;
  text-align: center;
  font-size: 30px;
  color: #999;
}
</style>